<template>
  <div class="dashboard-container">
    <el-tabs
      v-loading="loading"
      element-loading-text="加载中..."
    >
      <!-- 用户管理 -->
      <el-tab-pane label="用户管理">
        <el-button @click="showDialog = true">添加</el-button>
        <el-table
          :data="listData"
          style="width: 100%"
          border
          stripe
        >
          <el-table-column
            type="index"
            align="center"
            label="序号"
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="角色"
            prop="name"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="description"
            label="描述"
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="操作"
          >
            <template v-slot="{row}">
              <el-button type="success" size="small" @click="onShowAsignPermDialog(row.id)">分配角色</el-button>
              <el-button type="primart" size="small" @click="onuPdate(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="onDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 页码 -->
        <el-row type="flex" class="row-bg" justify="end">
          <div class="block">
            <el-pagination
              layout="prev, pager, next"
              :page-size="page.pagesize"
              :total="page.total"
              @current-change="onCurrentChange"
            >
            </el-pagination>
          </div>
        </el-row>
      </el-tab-pane>

      <!-- 配置管理 -->
      <el-tab-pane label="配置管理">
        <!-- 提示文案 -->
        <el-alert
          title="消息提对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改示的文案"
          type="info"
          show-icon
        >
        </el-alert>
        <!-- 表单 -->
        <el-form label-width="100px" style="width: 400px;margin-top: 20px;">
          <el-form-item label="公司名称">
            <el-input v-model="form.name" disabled></el-input>
          </el-form-item>
          <el-form-item label="公司地址">
            <el-input v-model="form.companyAddress" disabled></el-input>
          </el-form-item>
          <el-form-item label="公司邮箱">
            <el-input v-model="form.mailbox" disabled></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remarks" disabled></el-input>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <!-- 弹窗 -->
    <el-dialog :visible="showDialog" v-bind="$attrs" :title="formData.id? '编辑角色' : '新增角色'" v-on="$listeners" @open="onOpen" @close="onClose">
      <el-form ref="formRef" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入角色名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formData.description" placeholder="请输入角色描述" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-row type="flex" justify="center">
          <el-button @click="onClose">取消</el-button>
          <el-button type="primary" @click="handelConfirm">确定</el-button>
        </el-row>
      </div>
    </el-dialog>
    <!-- 分配角色 -->
    <el-dialog
      title="分配权限"
      :visible.sync="showAsignPermDialog"
    >
      <el-tree
        v-if="showAsignPermDialog"
        ref="permRef"
        :data="permList"
        :props="{label: 'name'}"
        show-checkbox
        default-expand-all
        :default-checked-keys="selectedPermList"
        check-strictly
        node-key="id"
      ></el-tree>
      <el-row slot="footer" type="flex" justify="center">
        <el-button type="primary" size="small" @click="onAsignPerm">
          确定
        </el-button>
        <el-button type="primary" size="small" @click="showAsignPermDialog = false">
          取消
        </el-button>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import { getCompanyInfoAPI, getRoleListAPI, addRoleAPI, deleteRoleAPI, updateRoleAPI, getRoleDetailAPI } from '@/api'
import { assignPerm } from '@/api/setting.js'
import { getChildren } from '@/utils'
import { getPermissionList } from '@/api/permisson'
export default {
  name: 'SettingPage',
  components: {},
  data() {
    return {
      loading: false, // 加载
      listData: [],
      form: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      formData: {
        name: undefined,
        description: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }],
        description: []
      },
      showDialog: false,
      showAsignPermDialog: false, // 分配角色弹窗
      permList: [], // 定义分配角色数据
      selectedPermList: [], // 选中的权限列表
      currentId: undefined // 当前操作的角色
    }
  },
  async created() {
    this.form = await getCompanyInfoAPI(this.$store.state.user.userinfo.companyId)
    this.getRoleList()
  },
  mounted() {},
  methods: {
    // 获取数据
    async getRoleList() {
      const { rows, total } = await getRoleListAPI(this.page)
      this.listData = rows
      this.page.total = total
    },
    // 点击页码
    onCurrentChange(val) {
      console.log(val)
      this.page.page = val
      this.getRoleList()
    },
    // 弹窗
    onOpen() {},
    onClose() {
      this.showDialog = false
      this.formData = this.$options.data().formData
      // this.formData = {
      //   name: undefined,
      //   description: undefined
      // }
    },
    // 弹窗确定
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        // 判断是编辑还是新增
        this.formData.id ? await updateRoleAPI(this.formData) : await addRoleAPI(this.formData)
        await this.getRoleList()
        this.onClose()
        this.$message.success('操作成功')
      })
    },
    // 删除
    onDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        console.log(id)
        await deleteRoleAPI(id)
        await this.getRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((err) => {
        console.log(err)
        this.$message({
          type: 'info',
          message: '删除失败'
        })
      })
    },
    // 编辑
    async onuPdate(id) {
      console.log(id)
      this.formData = await getRoleDetailAPI(id)
      this.showDialog = true
    },
    // 分配角色、
    async onShowAsignPermDialog(id) {
      this.loading = true // 页面加载
      this.currentId = id // 保存当前操作的角色id
      this.permList = getChildren(await getPermissionList(), '0')
      // 获取当前角色的权限
      const { permIds } = await getRoleDetailAPI(id)
      this.selectedPermList = permIds
      this.loading = false // 页面加载
      this.showAsignPermDialog = true
      console.log(permIds)
    },
    // 确定分配角色
    async onAsignPerm() {
      const permIds = this.$refs.permRef.getCheckedKeys()
      await assignPerm({
        id: this.currentId,
        permIds
      })
      this.$message.success('操作成功')
      this.showAsignPermDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #bbdbaa;
  }
}

</style>
